/*---------------*/
class Storage{
    static set(key, value, expiredTime){
        let duration = 60;
        if(expiredTime){
            duration = expiredTime;
        };
        let time = new Date().getTime() + duration * 1000;
        let data = {value: value, expiredTime: time};
        
        return new Promise((resolve, reject)=>{
            wx.setStorage({
                key: key,
                data: data,
                success: function(){
                    resolve();
                },
                fail: function(){
                    reject();
                }
            })
        });

    }
    static get(key){
        // console.log(key);
        return new Promise((resolve, reject)=>{
            wx.getStorage({
                key: key,
                success: function(res){
                    let expiredTime = res.data.expiredTime;
                    let now = new Date().getTime();
                    if(now < expiredTime){
                        // console.log("------从缓存中获取-------")
                        resolve(res.data.value);
                    }else{
                        // console.log("-----已经过期------");
                        resolve(null);
                    };
                },
                fail:function(res){
                    // console.log("--------缓存中没有-------");
                    resolve(null);
                }
            })
        });
    }
};

module.exports = Storage;